package com.training.controller;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Role;
import com.training.common.model.User;
import com.training.service.RoleService;
import com.training.service.RoleServiceImp;
import com.training.service.UserService;
import com.training.service.UserServiceImp;

import java.util.Date;
import java.util.List;

public class UserController extends Controller {

    @Inject(UserServiceImp.class)
    private UserService userService;

    @Inject(RoleServiceImp.class)
    private RoleService roleService;

    public void index(){
        render("/views/user/index.html");
    }

    public void getUserPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String username = getPara("username");
        Page<User> menuList = userService.getUserPage(pageNumber,pageSize,username);
        Result result = new Result();
        result.setData(new TrainingPage(menuList));
        renderJson(result);
    }

    public void showUser(){
        List<Role> roleList = roleService.getRoleList();
        set("roleList",roleList);
        if(getPara("ID")==null){
            render("/views/user/add.html");
        }else{
            String id = getPara("ID");
            setAttr("ID",id);
            render("/views/user/edit.html");
        }
    }

    public void addUser(){
        String username = getPara("username");
        String password = getPara("password");
        String role_id = getPara("role_id");
        User createuser = getSessionAttr("trainingsession");
        User user = new User();
        user.setUsername(username);
        Digester sha = new Digester(DigestAlgorithm.SHA256);
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHexpassword = sha.digestHex(md5.digestHex(password));
        user.setPassword(digestHexpassword);
        user.setRoleId(Integer.parseInt(role_id));
        user.setCreateuser(createuser.getID());
        user.setCreatetime(new Date());
        boolean boo = userService.addUser(user);
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void getUser(){
        String ID = getPara("ID");
        User user = User.dao.findById(ID);
        Result result = new Result();
        result.setData(user);
        renderJson(result);
    }

    public void editUser(){
        String ID = getPara("ID");
        String username = getPara("username");
        String password = getPara("password");
        String role_id = getPara("role_id");
        Result result = new Result();
        if(ID.equals("1")){
            result.setErrorNo(500);
            result.setErrorInfo("更新失败,管理员用户不允许修改！");
        }else {
            User user = User.dao.findById(ID);
            user.setUsername(username);
            user.setRoleId(Integer.parseInt(role_id));
            user.setCreatetime(new Date());
            boolean boo = userService.updateUser(user);
            if (boo) {
                result.setErrorNo(0);
                result.setErrorInfo("更新成功！");
            } else {
                result.setErrorNo(500);
                result.setErrorInfo("更新失败！");
            }
        }
        renderJson(result);
    }

    public void deleteUser(){
        String ids = getPara("ID");
        String[] idss = ids.split(",");
        boolean ifboo = true;
        for (String s : idss) {
            if (s.equals("1")){
                ifboo = false;
            }
        }
        Result result = new Result();
        if(ifboo){
            boolean boo = false;
            for (String id : idss){
                int idi = Integer.parseInt(id);
                boo = userService.deleteUser(idi);
            }
            if(boo){
                result.setErrorNo(0);
                result.setErrorInfo("删除成功！");
            }else{
                result.setErrorNo(500);
                result.setErrorInfo("删除失败！");
            }
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败，管理员用户不允许删除！");
        }
        renderJson(result);
    }

    public void showupdatepassword(){
        setAttr("ID",getPara("ID"));
        render("/views/user/updatepassword.html");
    }

    public void updatepassword(){
        String ID = getPara("ID");
        String password = getPara("password");
        User user = User.dao.findById(ID);
        Digester sha = new Digester(DigestAlgorithm.SHA256);
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHexpassword = sha.digestHex(md5.digestHex(password));
        user.setPassword(digestHexpassword);
        boolean boo = user.update();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }
}
